namespace CISLib
{
    using System;

    public class KPIHelper
    {
        private KPIHelper()
        {
        }

        public static string[] BuildLastFourYearQuarters(int len)
        {
            string curYQ = GetCurrentShortYearQuarter();
            string vCurYQ = GetCurrentShortYearQuarter();
            int y = int.Parse(curYQ.Substring(0, 2));
            int q = int.Parse(curYQ.Substring(3));
            int[] box = new int[] { 1, 2, 3, 4 };
            string[] strs = new string[len];
            int k = 0;
            int i = 0;
            while (i < len)
            {
                int index = (q - k) - 1;
                if (index < 0)
                {
                    y--;
                    y = (y < 0) ? (y + 100) : y;
                    k -= 4;
                }
                index = (index < 0) ? (index + 4) : index;
                strs[i] = string.Format("{0}Q{1}", y.ToString("0#"), box[index]);
                i++;
                k++;
            }
            int n = -1;
            for (int j = 0; j < strs.Length; j++)
            {
                if (strs[j] == vCurYQ)
                {
                    n = j;
                    break;
                }
            }
            string s = strs[0];
            strs[0] = strs[n];
            strs[n] = s;
            return strs;
        }

        public static string GetCurrentShortYearQuarter()
        {
            string ss = null;
            string ShortYearQuarter = "{0}{1}";
            string year = DateTime.Now.Year.ToString().Substring(2);
            int month = DateTime.Now.Month;
            if ((month >= 1) && (month <= 3))
            {
                ss = string.Format(ShortYearQuarter, year, "Q1");
            }
            if ((month >= 4) && (month <= 6))
            {
                ss = string.Format(ShortYearQuarter, year, "Q2");
            }
            if ((month >= 7) && (month <= 9))
            {
                ss = string.Format(ShortYearQuarter, year, "Q3");
            }
            if ((month >= 10) && (month <= 12))
            {
                ss = string.Format(ShortYearQuarter, year, "Q4");
            }
            return ss;
        }
    }
}

